package tman.main;


	/* ===========================================================
	 * JFreeChart : a free chart library for the Java(tm) platform
	 * ===========================================================
	 *
	 * (C) Copyright 2000-2004, by Object Refinery Limited and Contributors.
	 *
	 * Project Info:  http://www.jfree.org/jfreechart/index.html
	 *
	 * This library is free software; you can redistribute it and/or modify it under the terms
	 * of the GNU Lesser General Public License as published by the Free Software Foundation;
	 * either version 2.1 of the License, or (at your option) any later version.
	 *
	 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
	 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
	 * See the GNU Lesser General Public License for more details.
	 *
	 * You should have received a copy of the GNU Lesser General Public License along with this
	 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
	 * Boston, MA 02111-1307, USA.
	 *
	 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
	 * in the United States and other countries.]
	 *
	 * -------------------------
	 * OverlaidBarChartDemo.java
	 * -------------------------
	 * (C) Copyright 2002-2004, by Object Refinery Limited.
	 *
	 * Original Author:  David Gilbert (for Object Refinery Limited);
	 * Contributor(s):   -;
	 *
	 * $Id: OverlaidBarChartDemo.java,v 1.36 2004/06/02 09:48:24 mungady Exp $
	 *
	 * Changes
	 * -------
	 * 20-Sep-2002 : Version 1 (DG);
	 * 11-Oct-2002 : Added tooltips, modified series colors, centered frame on screen (DG);
	 * 11-Feb-2003 : Fixed bug where category labels were not showing on the axis (DG);
	 * 08-Sep-2003 : Bug fix (DG);
	 * 02-Jun-2004 : Set the rendering order (DG);
	 *
	 */


	import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.xml.parsers.ParserConfigurationException;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.DatasetRenderingOrder;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.renderer.xy.ClusteredXYBarRenderer;
import org.jfree.chart.renderer.xy.XYBarRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.statistics.HistogramDataset;
import org.jfree.data.statistics.HistogramType;
import org.jfree.data.xy.IntervalXYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import org.xml.sax.SAXException;

import common.configuration.Configuration;
import common.peer.PeerAddress;
import common.simulation.scenarios.Scenario;
import common.simulation.scenarios.Scenario2;



	/**
	 * A simple demonstration application showing how to create a bar chart overlaid
	 * with a line chart.
	 */
	public class GraphPanel2 extends ApplicationFrame {

	    /**
	     * Default constructor.
	     *
	     * @param title  the frame title.
	     */
		
		final JFreeChart chart;
		public JFreeChart getChart()
		{
			return chart;
		}
	    public GraphPanel2(final String title, HashMap<Integer, Integer> fan_in, HashMap<Integer, Integer> fan_out) {

	        super(title);

	        // create the first dataset...
	        //DefaultCategoryDataset dataset1 = new DefaultCategoryDataset();
	        //XYSeriesCollection intervalXYDataSet  = new XYSeriesCollection();
	        //int countPlotPoints = populateBandData(intervalXYDataSet, generateOptionsDataSet(fan_in, fan_out));
	        CategoryDataset dataset = new DefaultCategoryDataset();
	        dataset = createDataset(fan_in, fan_out);
	        chart = createClusteredChart("Fan-in/Fan-out", "Number of Peers", "Frequency", createXYDataset(fan_in, fan_out));
	        
	        chart.setTitle(title);
	        //--------------
	        //NumberAxis axis = (NumberAxis)chart.getXYPlot().getDomainAxis();
	        //axis.setTickUnit(new NumberTickUnit(1.0));
	        
	      //  chart.setLegend(new StandardLegend());

	        // add the chart to a panel...
	        final ChartPanel chartPanel = new ChartPanel(chart);
	        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
	        getContentPane().add(chartPanel);
	        JButton submit = new JButton("Back to main menu");

	       /* submit.addActionListener(new ActionListener() {
	          public void actionPerformed(ActionEvent e) {
	              String filename = "toverlay1.graphml";
	              try {
					new OverlayPanel(filename);
				} catch (ParserConfigurationException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (SAXException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}  
	          }
	        });*/
	        
	        JPanel controls = new JPanel();
	        controls.add(submit, BorderLayout.EAST);
	        getContentPane().add(controls, BorderLayout.EAST);
	        setPreferredSize(new java.awt.Dimension(900, 570));

	    }
	    
	    private static JFreeChart createClusteredChart(String title, String categoryAxisLabel, String valueAxisLabel, IntervalXYDataset dataset) {

	    	  NumberAxis domainAxis = new NumberAxis(categoryAxisLabel);
	    	  domainAxis.setAutoRangeIncludesZero(false);

	    	  ValueAxis valueAxis = new NumberAxis(valueAxisLabel);

	    	  XYBarRenderer renderer = new ClusteredXYBarRenderer();

	    	  XYPlot plot = new XYPlot(dataset, domainAxis, valueAxis, renderer);
	    	  plot.setOrientation(PlotOrientation.VERTICAL);

	    	  JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, true);

	    	  return chart;
	    	}
	    
	    private static HashMap<Integer,ArrayList<Integer>> generateOptionsDataSet(HashMap<Integer, Integer> fan_in, HashMap<Integer, Integer> fan_out)
	    {
	    	  ArrayList<Integer> categories = new ArrayList<Integer>();
	    	  HashMap<Integer,ArrayList<Integer>> OptionsDataSet = new HashMap<Integer,ArrayList<Integer>>();
	    	  for (Integer n : fan_in.keySet())
	    	  {
	    		  categories.add(n);
	    	  }
	    	  for (Integer n : fan_out.keySet())
	    	  {
	    		  categories.add(n);
	    	  }
	    	  Set<Integer> tmp = new HashSet<Integer>();
	    	  tmp.addAll(categories);
	    	  categories = new ArrayList<Integer>(tmp);
	    	  Collections.sort(categories);
	    	  for (Integer n : categories)
	    	  {
	    		  ArrayList<Integer> values = new ArrayList<Integer>();
	    		  
	    		  if(fan_in.get(n) != null)
	    			  values.add(fan_in.get(n));
	    		  else values.add(0);
	    		  if(fan_out.get(n) != null)
	    			  values.add(fan_out.get(n));
	    		  else values.add(0);
	    		  OptionsDataSet.put(n, values);
	    	  }
	    	  
	    	  return OptionsDataSet;
	    }
	    private static CategoryDataset createDataset(HashMap<Integer, Integer> fan_in, HashMap<Integer, Integer> fan_out) {
	    	  String series1 = "Fan-in";
	    	  String series2 = "Fan-out";
	    	  ArrayList<Integer> categories = new ArrayList<Integer>();
	    	  
	    	  for (Integer n : fan_in.keySet())
	    	  {
	    		  categories.add(n);
	    	  }
	    	  for (Integer n : fan_out.keySet())
	    	  {
	    		  categories.add(n);
	    	  }
	    	  Set<Integer> tmp = new HashSet<Integer>();
	    	  tmp.addAll(categories);
	    	  categories = new ArrayList<Integer>(tmp);
	    	  Collections.sort(categories);
	    	  
	    	  DefaultCategoryDataset dataset = new DefaultCategoryDataset();
	    	  
	    	  for (Integer n : categories)
	    	  {
	    		  int value;
	    		  if(fan_in.get(n) != null)
	    			  value = fan_in.get(n);
	    		  else value = 0;
	    		  dataset.addValue(value, series1, n.toString());
	    	  }
	    	  for (Integer n : categories)
	    	  {
	    		  int value;
	    		  if(fan_out.get(n) != null)
	    			  value = fan_out.get(n);
	    		  else value = 0;
	    		  dataset.addValue(value, series2, n.toString());
	    	  }

	    	  return dataset;
	    	}
	    
	    private static XYSeriesCollection createXYDataset(HashMap<Integer, Integer> fan_in, HashMap<Integer, Integer> fan_out) {
	    	  XYSeriesCollection xySeriesCollection = new XYSeriesCollection();
	    	  XYSeries series = new XYSeries("Fan-in");
	    	  XYSeries series2 = new XYSeries("Fan-out");
	    	  ArrayList<Integer> categories = new ArrayList<Integer>();
	    	  
	    	  for (Integer n : fan_in.keySet())
	    	  {
	    		  categories.add(n);
	    	  }
	    	  for (Integer n : fan_out.keySet())
	    	  {
	    		  categories.add(n);
	    	  }
	    	  Set<Integer> tmp = new HashSet<Integer>();
	    	  tmp.addAll(categories);
	    	  categories = new ArrayList<Integer>(tmp);
	    	  Collections.sort(categories);
	    	  
	    	  DefaultCategoryDataset dataset = new DefaultCategoryDataset();
	    	  
	    	  for (Integer n : categories)
	    	  {
	    		  Integer value = 0;
	    		  if(fan_in.get(n) != null)
	    			  series.add(n, fan_in.get(n));
	    		  else series.add(n, value);
	    	  }
	    	  for (Integer n : categories)
	    	  {
	    		  Integer value = 0;
	    		  if(fan_out.get(n) != null)
	    			  series2.add(n, fan_out.get(n));
	    		  else series2.add(n, value);
	    	  }
	          xySeriesCollection.addSeries(series);
	          xySeriesCollection.addSeries(series2);
	          return xySeriesCollection;
	    	}
	    
	    public <T> List<T> union(List<T> list1, List<T> list2) {
	        Set<T> set = new HashSet<T>();

	        set.addAll(list1);
	        set.addAll(list2);

	        return new ArrayList<T>(set);
	    }


	    /**
	     * Starting point for the demonstration application.
	     *
	     * @param args  ignored.
	     */
	   /* public static void main(final String[] args) {

	        final graph_panel demo = new graph_panel("Simulation Statistics");
	        demo.pack();
	        RefineryUtilities.centerFrameOnScreen(demo);
	        demo.setVisible(true);

	    }*/

	}
